﻿function CheckedAll(CheckBox)
{            
    var inputs = document.forms[0].elements;
    for (var i=0; i < inputs.length; i++)
    if (inputs[i].type == 'checkbox')
    {
        if (CheckBox.checked)
            inputs[i].checked = true;
       else
            inputs[i].checked =false;
    }
 }

 function getSelectedItem(radiobuttonlist) {
     var list = document.getElementById(radiobuttonlist);
     var options = list.getElementsByTagName("input");
     var str = "";
     for (x = 0; x < options.length; ++x) {
         if (options[x].type == "radio" && options[x].checked) {
             str = options[x].value;
             break;
         }
     }
     return str;
 }    
 
String.prototype.trim = function()
{
  return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function()
{
  return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function()
{
  return this.replace(/\s+$/,"");
}

function getleft(str,count) 
{
  var sl=str;
  sl = sl.substring(0,count); 
  return sl; 
}

function getright(str,count) 
{
  var sr=str;
  sr = sr.substring(sr.length-count,sr.length); 
  return sr; 
}

function delay(numberMillis)
{ 
  var now = new Date(); 
  var exitTime = now.getTime()+numberMillis;
  while(true)
  { 
     now = new Date(); 
     if(now.getTime() > exitTime) 
        return; 
  } 
}        

function setCookie(key,value)
{
   document.cookie = key+"="+escape(value);   
}  

function ReadCookie(name)
{
    var mycookie = document.cookie; 
    var start1 = mycookie.indexOf(name + "=");
    if (start1== -1)
      return "";
    else
    {
        start=mycookie.indexOf("=",start1)+1; 
        var end = mycookie.indexOf(";",start);
         if (end==-1)
         {
             end=mycookie.length;
         }
         var value=unescape(mycookie.substring(start,end));
         if (value==null)
         {
            return "";
         }
         else
         {
            return value;
         }
     }
}  

function ComputeTime(n) 
{ 
    var mei = new Date(); 
    mei.setTime(mei.getTime() + n*1000); 
    var h = (mei.getHours() > 9) ? mei.getHours() : "0" + mei.getHours(); 
    var m = (mei.getMinutes() > 9) ? mei.getMinutes() : "0" + mei.getMinutes(); 
    var s = (mei.getSeconds() > 9) ? mei.getSeconds() : "0" + mei.getSeconds(); 
    return h +":"+ m +":"+ s +" ";
} 

Date.prototype.dateDiff = function(interval,objDate)
{
  if(arguments.length<2||objDate.constructor!=Date) return 0;
  switch (interval) 
  {
      //計算秒差
      case "s":return parseInt((objDate-this)/1000);
      //計算分差
      case "n":return parseInt((objDate-this)/60000);
      //計算時差
      case "h":return parseInt((objDate-this)/3600000);
      //計算日差
      case "d":return parseInt((objDate-this)/86400000);
      //計算週差
      case "w":return parseInt((objDate-this)/(86400000*7));
      //計算月差
      case "m":return (objDate.getMonth()+1)+((objDate.getFullYear()-this.getFullYear())*12)-(this.getMonth()+1);
      //計算年差
      case "y":return objDate.getFullYear()-this.getFullYear();
      //輸入有誤
      default:return 0;
  }
} 

var sDT = new Date("2000/01/01 08:00:00");
var eDT = new Date();
        
var DiffValue = sDT.dateDiff("s",eDT);

function subString(str, len, hasDot)  
{  
    var newLength = 0;  
    var newStr = "";  
    var chineseRegex = /[^\x00-\xff]/g;  
    var singleChar = "";  
    var strLength = str.replace(chineseRegex,"**").length;  
    for(var i = 0;i < strLength;i++)  
    {  
        singleChar = str.charAt(i).toString();  
        if(singleChar.match(chineseRegex) != null)  
        {  
            newLength += 2;  
        }      
        else  
        {  
            newLength++;  
        }  
        if(newLength > len)  
        {  
            break;  
        }  
        newStr += singleChar;  
    }  
    if(hasDot && strLength > len)  
    {  
        newStr += "...";  
    }  
    return newStr;  
}

function MustInt(obj) {      
    var intvalue = parseInt(obj.value);
    if (isNaN(intvalue)) {
        obj.value = "0";
    }
    else {    
        while ((obj.value.length > 0) && (getleft(obj.value, 1) == "0")) {
            obj.value = getright(obj.value, obj.value.length - 1);
        }
        while ((obj.value.length > 0) && (getleft(obj.value, 1) == ".")) {
            obj.value = "0";
        }
        obj.value = parseInt(obj.value);        
        if (isNaN(obj.value)) {
            obj.value = "0";
        }
        if (parseInt(obj.value) < 0) {
            obj.value = "0";
        }
    }
}

///数值
function MustFloat(obj, digit) {
    var num = 1;
    for (i = 0; i < digit; i++) {
        num = num * 10;
    }

    var floatvalue = parseFloat(obj.value);
    if (isNaN(floatvalue)) {
        obj.value = "0";
    }
    else {
        obj.value =Math.abs(Math.round(floatvalue * num) / num);
    }
}

///字符限制
function WordLimit(obj,count)
{
   obj.value = obj.value.replace(/[<>]+/g, '');
   var maxlength = new Number(count);
   var str = obj.value;
   var ilength = str.replace(/[^\x00-\xff]/g,"xx").length;
   if (ilength>maxlength)
   {
       obj.value = subString(str, maxlength-3, true);
       ShowMsgByLanaguage("Only "+maxlength +" chars","隻允許"+maxlength+"個字符");
  }
}

function ShowMsg(strMsg) 
{
    alert(strMsg);
}

function ShowMsgByLanaguage(english,chinese)
{
   var culture = ReadCookie("culture");
   if (culture=="en-us")
   {
      alert(english);
   }
   else
   {
     alert(chinese);
   }   
}

function GetMsgByLanaguage(english,chinese)
{
   var str = "";
   var culture = ReadCookie("culture");
   if (culture=="en-us")
   {
      str = english;
   }
   else
   {
     str = chinese;
   }   
   return str;
}


////Forbid mouse rightclick.
//document.oncontextmenu=function()
//{ 
//   return false;
//}

document.onkeydown=function(){
    
    if (event.keyCode == 13) {
        return event.keyCode = 9;
    }

    if (event.keyCode == 0) {
        return event.keyCode = 13;
    }
    
//    The point
//    if (event.keyCode == 110) {
//        event.keyCode = 0;
//        event.cancelBubble = true;
//        return false;
//    }

//    if (event.keyCode == 116) {
//        event.keyCode = 0;
//        event.cancelBubble = true;
//        return false;
//    }
}

function EnterForText() {
    if (event.keyCode == 13) {
        return event.keyCode = 0; 
     }
}

//dont show javascript error
//function ResumeError() 
//{ 
//   return true; 
//} 

//window.onerror = ResumeError; 
